<?php
#基础函数
include_once('_path.php');
include_once("../includes/init.php");

$quote = array();

#判断变量
if($_GET["func"]!="edit"){
	die("func error.");
}
	#如果是编辑
	$action = "edit";

	$vote_id = trim($_REQUEST['vote_id']);
	if(!is_numeric($vote_id)){
		die("The vote is not found");
	}
	$quote['vote_id'] = string_sql($vote_id);
	$sql = "SELECT v.*, u.name FROM vote v LEFTJOIN user u ON u.id = v.user_id WHERE v.id = ".$quote['vote_id'];
	$rs = $database->queryArray($sql);
	if (sizeof($rs)!=1) {
		die("The vote is not found");
	}

	#预设变量
	$title = $rs[0]['title'];
	$url = $rs[0]['url'];
	$desc = $rs[0]['desc'];
	$add = $rs[0]['add'];
	$sub = $rs[0]['sub'];
	$views = $rs[0]['views'];
	$name = $rs[0]['name'];
	$images = $rs[0]['images'];
	$thumbfile = $rs[0]['thumbfile'];
	$create_time = $rs[0]['create_time'];
	$user_id = $rs[0]['user_id'];
	
if($_POST["do"]=="do"){
	#处理来源信息
    $title = trim($_REQUEST['title']);
    $quote['title'] = string_sql($title);
	$url = trim($_REQUEST['url']);
    $quote['url'] = string_sql($url);
	$desc = trim($_REQUEST['desc']);
    $quote['desc'] = string_sql($desc);
	$add = trim($_REQUEST['add']);
    $quote['add'] = string_sql($add);
	$sub = trim($_REQUEST['sub']);
    $quote['sub'] = string_sql($sub);
	$views = trim($_REQUEST['views']);
    $quote['views'] = string_sql($views);
	#判断内容
	verify_field("title", "Title", "empty");
	verify_field("url", "Url", "empty");
	verify_field("url", "Url", "url");
	verify_field("desc", "Descrption", "empty");
	verify_field("add", "Yes", "empty");
	verify_field("add", "Yes", "number_only");
	verify_field("sub", "NO", "empty");
	verify_field("sub", "NO", "number_only");
	verify_field("views", "Views", "empty");
	verify_field("views", "Views", "number_only");

	#如果没有错误，正式提交数据库
	if (!count($error_strs)) {
		#如果是编辑
			$sql = "UPDATE vote SET ".
				"title = ".$quote['title'].", url = ".$quote['url'].", desc = ".$quote['desc'].
				", add = ".$quote['add'].", sub = ".$quote['sub'].
				", views = ".$quote['views']." WHERE id = ".$quote['vote_id'];
			$database->execute($sql);

			add_log('do_edit', 'vote', "edit vote id is $vote_id");
		$_REQUEST['referrer'] = "./";
		redirect("OK");
	}
}

$css = array('index', 'table','form');
$js = array();
include_once("../layout/_header.php");
?>
<div id="path">
	<ul>
		<li><a href="<?php echo _CONST_WEB_URL ?>"><?php echo _CONST_WEB_NAME?></a></li>
		<li><a href="<?php echo _CONST_BACKEND_URL ?>index.php">Backend</a></li>
		<?php
			add_log('edit', 'vote', "edit vote id is $vote_id");
        ?>
			<li><a href="./">vote</a></li>
			<li>Edit &quot;<?php echo $title ?>&quot; vote</li>
	</ul>
<a href="<?php echo _CONST_BACKEND_URL ?>logout.php" class="r" style="margin:2px 10px"><span class="logout">Logout</span></a>
</div>

<div id="content">
	<ul id="submenu">
		<li <?php echo iif($action == "", 'class="current"') ?>><a href="./" class="button"><span class="list">List</span></a></li>
		<?php
		if ($action == "edit") {
			?><li class="current"><a href="vote_edit.php?func=edit&vote_id=<?php echo $vote_id ?>" class="button"><span class="edit">Edit</span></a></li>
		<?php
		}
		?>
	</ul>
   <span style="margin:7px 10px 2px 50px;" class="l">Current vote:<?php echo $_SESSION['bname']?>&nbsp;&nbsp;<?php echo gmdate('D M j, Y h:ia T', _CONST_TIMENOW) ?></span>

	<div class="cr"></div>
		<form action="vote_edit.php?func=<?php echo $action?>" method="post" id="form" class="form border" name="form">
			<input type="hidden" name="do" value="do" />
			<input type="hidden" name="vote_id" value="<?php echo $vote_id ?>" />
			<fieldset>
			<legend><span>vote Information</span></legend>
			<ol>
				<li>
					<label for="title">Title
						<em><img src="<?php echo _CONST_BACKEND_URL ?>media/required.gif" alt="required" /></em>
						<?php echo show_field_error('title') ?>
					</label>
					<input name="title" type="text" class="text" size="30" maxlength="150" value="<?php echo htmlspecialchars($title) ?>" />
				</li>
				<li>
					<label for="url">Url
						<em><img src="<?php echo _CONST_BACKEND_URL ?>media/required.gif" alt="required" /></em>
						<?php echo show_field_error('url') ?>
					</label>
					<input name="url" type="text" class="text" size="30" maxlength="150" value="<?php echo htmlspecialchars($url) ?>" />
				</li>
				<li>
					<label for="desc">Description
					<em><img src="<?php echo _CONST_BACKEND_URL ?>media/required.gif" alt="required" /></em>
						<?php echo show_field_error('desc') ?>
					</label>
					<textarea name="desc" class="textarea" rows="5" cols="30"><?= htmlspecialchars($desc) ?></textarea>
				</li>
				<li>
					<label for="add">Yes <?= show_field_error('add') ?></label>
					<input name="add" type="text" class="text" size="5" maxlength="4" style="width:50px" value="<?= htmlspecialchars($add) ?>" />
				</li>
				<li>
					<label for="sub">NO <?= show_field_error('sub') ?></label>
					<input name="sub" type="text" class="text" size="5" maxlength="4" style="width:50px" value="<?= htmlspecialchars($sub) ?>" />
				</li>
				<li>
					<label for="views">Views <?= show_field_error('views') ?></label>
					<input name="views" type="text" class="text" size="5" maxlength="4" style="width:50px" value="<?= htmlspecialchars($views) ?>" />
				</li>
				<li>
					<label for="name">User Name
					</label>
					<?php echo htmlspecialchars($name)?>
				</li>
				<li>
					<label for="images">Images</label>
					<?php if (file_exists(_CONST_PIC_PATH.'vote/'.$user_id."/2_".$images)) { ?>
						<img src="<?php echo _CONST_PIC_URL.'avatar/'.$user_id."/2_".$images ?>" class="" style="background-color:#fff;border:1px dotted #999;padding:1px;margin-top:10px;" />
					<?php } ?>
				</li>	
				<li>
					<label for="thumbfile">Thumbfile</label>
					<?php if (file_exists(_CONST_PIC_PATH.'avatar/'.$user_id."/2_".$thumbfile)) { ?>
						<img src="<?php echo _CONST_PIC_URL.'avatar/'.$user_id."/2_".$thumbfile ?>" class="" style="background-color:#fff;border:1px dotted #999;padding:1px;margin-top:10px;" />
					<?php } ?>	
				</li>	
				<li>
					<label for="createtime">Createtime
					</label>
					<?php echo $create_time?>
				</li>
			</ol>
			</fieldset>
			<fieldset class="submit">
			<input type="submit" class="button" value="Edit" />
			<input type="button" value="Cancel" class="button" onclick="location.href='<?php echo _CONST_BACKEND_URL ?>vote/index.php'"/>
			</fieldset>
		</form>
</div>
<?php
include_once("../layout/_footer.php");
?>